﻿using Ext.Net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PatrolShop_ShopInspectionList : ExtBaseRpt
{
    mrjapp mrj = new mrjapp();
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            try
            {
                InsActLog(mid, "浏览");
                //分部
                //SELECT vs.OperationAreaName,OAID,AreaID FROM KM_V_Ins_ShopList vs where CompanyID = 'KM'
                OperationAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT distinct vs.OperationAreaName FROM KM_V_Ins_ShopList vs where CompanyID = 'KM'");//,OAID,AreaID 
                //txtBigAreaName.GetStore().DataSource = mydb.DM_GetDateSet("select distinct BigAreaName From V_ShopList where CompanyID='KM'");
                //txtAreaName.GetStore().DataSource = mydb.DM_GetDateSet("select distinct AreaName From V_ShopList where CompanyID='KM'");
                TxtStoreManager.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT tif_shopManager FROM  t_inspection_finishMaster WHERE isRemove=0 GROUP BY tif_shopManager ");
                txtSupervisor.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT tif_supervisor FROM  t_inspection_finishMaster WHERE isRemove=0 GROUP BY tif_supervisor ");
                //时间默认当前月
                string DateNow = DateTime.Now.ToString("yyyy-MM-01");
                this.txtTimeBegin.Text = DateNow;
                this.txtTimeEnd.Text = Convert.ToDateTime(DateNow).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"); // DateTime.Now.ToString("yyyy-MM-dd");

            }
            catch (Exception ex)
            {
                ShowBox("错误：" + ex.Message);
            }
        }
        if (Session["UserID"] == null)
        {
            ShowBox("用户登入超时请求重新登入");
        }
    }
    //获取省份
    protected void GetBigAreaName(object sender, DirectEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(OperationAreaName.Text))
        {
            txtBigAreaName.Text = string.Empty;
            txtAreaName.Text = string.Empty;
        }
        else
        {
            string sql = string.Format(@"select distinct BigAreaName From V_ShopList where CompanyID='KM' and AreaID in( 
                SELECT distinct AreaID FROM KM_V_Ins_ShopList vs where CompanyID='KM' and vs.OperationAreaName='{0}')", OperationAreaName.Text);
            txtBigAreaName.GetStore().DataSource = mydb.DM_GetDateSet(sql);
            //先清空数据后再绑定
            txtBigAreaName.Text = string.Empty;
            txtAreaName.Text = string.Empty;
            this.txtBigAreaName.DataBind();
        }

    }

    //获取城市
    protected void GetArea(object sender, DirectEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtBigAreaName.Text))
        {
            txtAreaName.Text = string.Empty;
        }
        else
        {
            txtAreaName.GetStore().DataSource = mydb.DM_GetDateSet("SELECT DISTINCT AreaName FROM V_ShopList WHERE BigAreaName='" + txtBigAreaName.Value + "'");
            //先清空数据后再绑定
            txtAreaName.Text = string.Empty;
            this.Store_AreaName.DataBind();
        }

    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null, null);
    }

    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        try
        {
            string txtTimeBegin = this.txtTimeBegin.Text;
            string txtTimeEnd = this.txtTimeEnd.Text;
            //日期查询
            if (string.IsNullOrEmpty(txtTimeBegin) || Convert.ToDateTime(txtTimeBegin) == Convert.ToDateTime("0001-01-01 00:00:00") || string.IsNullOrEmpty(txtTimeEnd) || Convert.ToDateTime(txtTimeEnd) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入开始日期和结束日期");
                return;
            }
            txtTimeBegin = Convert.ToDateTime(txtTimeBegin).ToString("yyyy-MM-dd");
            txtTimeEnd = Convert.ToDateTime(txtTimeEnd).ToString("yyyy-MM-dd");

            this.Store1.DataSource = GetDataList(txtTimeBegin, txtTimeEnd);
            this.Store1.DataBind();
        }
        catch (Exception ex)
        {
            Activity.Log.Error("请求报表数据异常：" + ex);
            ShowBox("查询出错：" + ex.Message);
        }
    }

    public string GetWhere()
    {
        string OperationAreaName = this.OperationAreaName.Text;
        string strBigAreaName = this.txtBigAreaName.Text;//省份
        string strAreaName = this.txtAreaName.Text;//市
        string strManager = this.TxtStoreManager.Text;
        string strSupervisor = this.txtSupervisor.Text;
        
        string strWhere = string.Empty;
        if (!string.IsNullOrEmpty(OperationAreaName))
        {
            strWhere += string.Format(@" AND vlist.OperationAreaName='{0}' ", OperationAreaName);
        }
        if (!string.IsNullOrEmpty(strBigAreaName))
        {
            strWhere += string.Format(@" AND vshop.BigAreaName='{0}' ", strBigAreaName);
        }
        if (!string.IsNullOrEmpty(strAreaName))
        {
            strWhere += string.Format(@" AND vshop.AreaName='{0}' ", strAreaName);
        }
        if (!string.IsNullOrEmpty(strManager))
        {
            strWhere += string.Format(@" AND mast.tif_shopManager='{0}' ", strManager);
        }
        if (!string.IsNullOrEmpty(strSupervisor))
        {
            strWhere += string.Format(@" AND mast.tif_supervisor='{0}' ", strSupervisor);
        }

        return strWhere;
    }

    //获取列表数据
    public DataSet GetDataList(string TimeBegin, string TimeEnd)
    {
        string strWhere = GetWhere();
        string sql = string.Format(@"SELECT 
	ROW_NUMBER() OVER (ORDER BY mast.tif_checkDate DESC) AS rowNum,
	mast.tif_id,
	mast.tif_shopName,
	mast.tif_shopCode,
	--(vshop.BigAreaName+'/'+vshop.AreaName) as AreaNames,
	vlist.OperationAreaName,
	vshop.BigAreaName,
	vshop.AreaName,
	tif_checkDate=CONVERT(nvarchar(10),mast.tif_checkDate,120),
	mast.tif_shopManager,
	mast.tif_supervisor,
	mast.ProblemNum as tif_questionCount,
	tid_points=(isnull(mast.ins_score,0)-isnull(mast.new_tif_score,0)),
	mast.new_tif_score,
	mast.tif_summary,
	--(case ISNULL(img.ID,'') when '' then  '无' else '有' end) as Img_ID,
	tif_progress=ISNULL(mast.tif_progress,0)
FROM V_t_inspection_finishMaster mast
INNER JOIN V_ShopList vshop on vshop.CompanyID='KM' and vshop.shopcode=mast.tif_shopCode
LEFT JOIN KM_V_Ins_ShopList vlist on vlist.ShopCode=mast.tif_shopCode
LEFT JOIN t_inspection_finishDetail detail on mast.tif_id=detail.tid_id
-- left join t_inspection_img img on img.img_type=1 and detail.tid_id=img.tid_id
WHERE 1=1 
AND mast.tif_checkDate>='{0}' 
AND mast.tif_checkDate<='{1}' 
{2} ", TimeBegin, TimeEnd, strWhere);

        return mydb.DM_GetDateSet(sql);
    }

    //导出EXCEL(未使用，有错)
    protected void Store_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        ExtGridToExcel(mid, "xls", e.Xml, this.Response, "店铺巡检列表");
    }

    protected void BtnOutExcel(object sender, DirectEventArgs e)
    {
        //导出EXCEL
        try
        {
            string txtTimeBegin = this.txtTimeBegin.Text;
            string txtTimeEnd = this.txtTimeEnd.Text;
            //日期查询
            if (string.IsNullOrEmpty(txtTimeBegin) || Convert.ToDateTime(txtTimeBegin) == Convert.ToDateTime("0001-01-01 00:00:00") || string.IsNullOrEmpty(txtTimeEnd) || Convert.ToDateTime(txtTimeEnd) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入开始日期和结束日期");
                return;
            }
            txtTimeBegin = Convert.ToDateTime(txtTimeBegin).ToString("yyyy-MM-dd");
            txtTimeEnd = Convert.ToDateTime(txtTimeEnd).ToString("yyyy-MM-dd");

            DataSet ds = GetExportData(txtTimeBegin, txtTimeEnd);
            string path = NpoiHelper.ExportByWeb(mid, ds, "店铺巡检列表");
            this.ResourceManager1.AddScript("outExcle('" + path + "')");
        }
        catch (Exception ex)
        {
            Activity.Log.Error("请求报表数据异常：" + ex);
            ShowBox("导出出错：" + ex.Message);
        }
    }
    //获取导出的列表数据
    public DataSet GetExportData(string TimeBegin, string TimeEnd)
    {
        string strWhere = GetWhere();
        string sql = string.Format(@"SELECT 
	mast.tif_shopName AS '店铺名称',
	mast.tif_shopCode AS '店铺代码',
	vlist.OperationAreaName AS '分部',
	vshop.BigAreaName AS '省份',
	vshop.AreaName AS '城市',
	mast.tif_shopManager AS '店经理',
	CONVERT(nvarchar(10),mast.tif_checkDate,120) AS '巡店时间',
	mast.tif_supervisor AS '巡店督导',
	mast.ProblemNum as '问题数',
	(isnull(mast.ins_score,0)-isnull(mast.new_tif_score,0)) AS '扣分',
	mast.new_tif_score AS '得分',
	mast.tif_summary AS '备注',
	case ISNULL(mast.tif_progress,0) when 0 then '未完成' else '已完成' end as '状态'
FROM V_t_inspection_finishMaster mast
INNER JOIN V_ShopList vshop on vshop.CompanyID='KM' and vshop.shopcode=mast.tif_shopCode
LEFT JOIN KM_V_Ins_ShopList vlist on vlist.ShopCode=mast.tif_shopCode
LEFT JOIN t_inspection_finishDetail detail on mast.tif_id=detail.tid_id
-- left join t_inspection_img img on img.img_type=1 and detail.tid_id=img.tid_id
WHERE 1=1  
AND mast.tif_checkDate>='{0}' 
AND mast.tif_checkDate<='{1}' 
{2} ", TimeBegin, TimeEnd, strWhere);

        sql = sql + " ORDER BY mast.tif_checkDate DESC ";
        return mydb.DM_GetDateSet(sql);
    }

    //逻辑删除未完成的巡检记录
    [DirectMethod(Namespace = "CompanyX")]
    public void DeleteTask(string JsonParams)
    {
        try
        {
            string txtTimeBegin = this.txtTimeBegin.Text;
            string txtTimeEnd = this.txtTimeEnd.Text;
            //日期查询
            if (string.IsNullOrEmpty(txtTimeBegin) || Convert.ToDateTime(txtTimeBegin) == Convert.ToDateTime("0001-01-01 00:00:00") || string.IsNullOrEmpty(txtTimeEnd) || Convert.ToDateTime(txtTimeEnd) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入开始日期和结束日期");
                return;
            }

            txtTimeBegin = Convert.ToDateTime(txtTimeBegin).ToString("yyyy-MM-dd");
            txtTimeEnd = Convert.ToDateTime(txtTimeEnd).ToString("yyyy-MM-dd");
            if (!string.IsNullOrEmpty(JsonParams))
            {
                string strWhere = string.Format(@"UPDATE t_inspection_finishMaster SET isRemove=1 WHERE tif_id={0} ", JsonParams);
                int rowNum = mydb.DM_ExecuteNonQuery(strWhere);
                if (rowNum > 0)
                {
                    this.Store1.DataSource = GetDataList(txtTimeBegin, txtTimeEnd);
                    this.Store1.DataBind();
                }
            }
            else
            {
                ShowBox("巡检任务ID不能为空！");
            }
        }
        catch (Exception ex)
        {
            ShowBox("操作出错：" + ex.Message);
        }
    }


}